package com.itany.corejava.day13;

import javax.swing.*;
import java.awt.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileEncryptor {

    private JFrame jf;
    private JLabel jl1;
    private JLabel jl2;
    private JTextField jtf1;
    private JTextField jtf2;
    private JCheckBox jcb;
    private JButton jb1;
    private JButton jb2;

    public FileEncryptor() {
        jf=new JFrame("文件加密器");
        jl1=new JLabel("文件：");
        jl2=new JLabel("密钥：");
        jtf1=new JTextField(20);
        jtf2=new JTextField(5);
        jcb=new JCheckBox("创建新文件");
        jb1=new JButton("打开");
        jb2=new JButton("加密/解密");
    }

    private void init(){
        JPanel jp1=new JPanel();
        jp1.add(jl1);
        jp1.add(jtf1);
        jp1.add(jb1);

        JPanel jp2=new JPanel();
        jp2.add(jl2);
        jp2.add(jtf2);
        jp2.add(jcb);
        jp2.add(jb2);

        jf.add(jp1, BorderLayout.NORTH);
        jf.add(jp2);

    }

    private void setStyle(){
        jtf1.setEditable(false);
    }

    private void addEventHandler(){
        jb1.addActionListener(e->{

            FileDialog fd=new FileDialog(jf,"打开文件",FileDialog.LOAD);

            fd.setVisible(true);

            if(fd.getFile()!=null){
                jtf1.setText(fd.getDirectory()+fd.getFile());
            }


        });

        jb2.addActionListener(e->{

            System.out.println(jtf2.getText());

            if("".equals(jtf1.getText())||"".equals(jtf2.getText())){
                JOptionPane.showMessageDialog(jf,"信息不完整");
                return;
            }


            if (jcb.isSelected()){
                System.out.println("选中");


                //如果文件名以.enc结尾表示加密后的文件，否则是未加密的
                //根据打开的文件名定义输入和输出流
                //加密解密规则，读取一个字节跟密钥做异或(a^b)写到加密的文件中

                try(FileInputStream fis=new FileInputStream(jtf1.getText());
                    FileOutputStream fos=new FileOutputStream( jtf1.getText().endsWith(".enc")
                                                               ?jtf1.getText().substring(0,jtf1.getText().lastIndexOf(".enc"))
                                                               :jtf1.getText()+".enc")){



                    int b;

                    while((b=fis.read())!=-1){
                        fos.write(b^Integer.parseInt(jtf2.getText()));
                    }





                    JOptionPane.showMessageDialog(jf,"加密/解密成功");
                }catch (NumberFormatException ex){
                    JOptionPane.showMessageDialog(jf,"密钥必须是数字");
                }catch (Exception ex){
                    JOptionPane.showMessageDialog(jf,"加密/解密失败");
                }



            }else{
                System.out.println("未选中");
            }



        });
    }

    public void show(){
        init();
        setStyle();
        addEventHandler();
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new FileEncryptor().show();
    }


}
